#*****************************************************************************
# DESCRIPTION: Dinotrace: Makefile pre-configure version
#
# This file is part of Dinotrace.
#
# Author: Wilson Snyder <wsnyder@wsnyder.org>
#
# Code available from: https://www.veripool.org/dinotrace
#
#*****************************************************************************
#
# This file is covered by the GNU public licence.
#
# Dinotrace is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Dinotrace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dinotrace; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#****************************************************************************/
#
# make all	to compile and build Dinotrace.
# make install	to install it.
# make TAGS	to update tags tables.
#
# make clean  or  make mostlyclean
#      Delete all files from the current directory that are normally
#      created by building the program.	 Don't delete the files that
#      record the configuration.  Also preserve files that could be made
#      by building, but normally aren't because the distribution comes
#      with them.
#
# make distclean
#      Delete all files from the current directory that are created by
#      configuring or building the program.  If you have unpacked the
#      source and built the program without creating any other files,
#      `make distclean' should leave only the files that were in the
#      distribution.
#
# make maintainer-clean
#      Delete everything from the current directory that can be
#      reconstructed with this Makefile.  This typically includes
#      everything deleted by distclean, plus more: C source files
#      produced by Bison, tags tables, info files, and so on.
#
# make extraclean
#      Still more severe - delete backup and autosave files, too.

#### Start of system configuration section. ####

abs_srcdir = @abs_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
HOST = @HOST@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
MAKEINFO = makeinfo
TEXI2DVI = texi2dvi
RST2HTML = rst2html
PERL = @PERL@

#### Don't edit: You're much better using configure switches to set these
prefix = @prefix@
exec_prefix = @exec_prefix@

# Directory in which to install scripts.
bindir = @bindir@

# Directory in which to install library files.
datadir = @datadir@

# Directory in which to install documentation info files.
infodir = @infodir@

# Directory in which to install package specific files
# Note this gets coded into src/config.h also
pkgdatadir = @pkgdatadir@

#### End of system configuration section. ####
######################################################################

SHELL = /bin/sh

SUBDIRS = src lisp

INFOS = dinotrace.info dinotrace.txt dinotrace.pdf

CPPCHECK = cppcheck
CPPCHECK_FLAGS = --enable=all --suppress=variableScope --inline-suppr

# Files that can be generated, but should be up to date for a distribution.
DISTDEP = $(INFOS) Makefile src/dinodoc.pl src/dist_date.pl
# Files to distribute.
DISTBIN = $(wildcard bin/dinotrace-*)
DISTFILES = $(INFOS) COPYING Changes configure *.in *.ac \
	config.sub \
	install-sh mkinstalldirs *.texi *.com \
	*.rst \
	docs/*.rst \
	docs/*.png \
	docs/TODO.txt \
	docs/html_texi_clean \
	src/*.in src/*.h src/*.c src/*.pl src/*.com lisp/* traces/* \
	$(DISTBIN)

all: dinotrace_exe info

test: dinotrace_exe
	./dinotrace traces/ascii.tra

obj_dir:
	-mkdir -p $@
obj_dbg:
	-mkdir -p $@
bin:
	-mkdir -p bin

.PHONY:dinotrace_exe
.PHONY:dinotrace_dbg
.PHONY:dinotrace
dinotrace_exe dinotrace: obj_dir bin
	@echo making dinotrace in obj_dir
	cd obj_dir && $(MAKE) -f $(abs_srcdir)/src/Makefile dinotrace
	(cp obj_dir/dinotrace ./dinotrace >/dev/null 2>&1\
	 || cp obj_dir/dinotrace.exe ./dinotrace.exe >/dev/null 2>&1)
	(cp obj_dir/dinotrace bin/dinotrace-$(HOST) >/dev/null 2>&1\
	 || cp obj_dir/dinotrace.exe bin/dinotrace-$(HOST).exe >/dev/null 2>&1)

dinotrace_dbg: obj_dbg
	@echo making dinotrace in obj_dbg
	cd obj_dbg && $(MAKE) DT_DEBUG=1 -f $(abs_srcdir)/src/Makefile dinotrace
	(cp obj_dbg/dinotrace ./dinotrace_dbg >/dev/null 2>&1\
	 || cp obj_dbg/dinotrace.exe ./dinotrace_dbg.exe >/dev/null 2>&1)

cppcheck: obj_dbg
	cd obj_dbg && $(CPPCHECK) $(CPPCHECK_FLAGS) -DDEBUG -I$(srcdir) ..

info: $(INFOS)

# Use --no-split to avoid creating filenames > 14 chars.
dinotrace.info: dinotrace.texi
	$(MAKEINFO) -I$(srcdir) $(srcdir)/dinotrace.texi --no-split --output=$@

dinotrace.txt: dinotrace.texi
	$(MAKEINFO) -I$(srcdir) $(srcdir)/dinotrace.texi --output=$@ \
	--no-headers --no-validate

README.html: README.rst
	$(RST2HTML) $< $@

docs/FAQ.html: docs/FAQ.rst
	$(RST2HTML) $< $@

%.html: %.texi
	perl docs/html_texi_clean $*.texi >$*.tmp
	texi2html -monolithic $*.tmp
	mv $*.tmp.html $*.html
	rm $*.tmp

%.pdf: %.texi
	perl docs/html_texi_clean $*.texi >$*.tmp
	texi2pdf $*.tmp
	rm $*.tmp

dvi: dinotrace.dvi

dinotrace.dvi: dinotrace.texi
	$(TEXI2DVI) $(srcdir)/dinotrace.texi

installdirs:
	$(SHELL) ${srcdir}/mkinstalldirs $(DESTDIR)$(bindir) $(DESTDIR)$(infodir)

install: all info installdirs install-info
	for p in dinotrace; do \
	  $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/$$p; \
	done
	@echo ""
	@echo "*** Lisp files not installed, you may want to"
	@echo "*** cd lisp ; make install"
	@echo ""
	@echo "Type 'setenv DISPLAY :0.0' then 'dinotrace' to test."

# Don't cd, to avoid breaking install-sh references.
install-info: info installdirs
	for i in *.info*; do \
	  $(INSTALL_DATA) $$i $(DESTDIR)$(infodir)/$$i; \
	done

uninstall:
	cd $(DESTDIR)$(bindir)  && rm -f dinotrace
	cd $(DESTDIR)$(infodir) && rm -f dinotrace.info*

# autoheader might not change config.h.in, so touch a stamp file.
${srcdir}/config.h.in: stamp-h.in
${srcdir}/stamp-h.in: configure.ac Makefile.in ${srcdir}/Makefile.in ${srcdir}/config.h.in
	cd ${srcdir} && autoheader
	echo timestamp > ${srcdir}/stamp-h.in
config.h: stamp-h
stamp-h: config.h.in config.status
	./config.status
Makefile: Makefile.in config.status
	./config.status
src/Makefile: src/Makefile.in config.status
	./config.status
config.status: configure
	./config.status --recheck
configure: configure.ac
	autoconf

maintainer-clean::
	@echo "This command is intended for maintainers to use;"
	@echo "rebuilding the deleted files requires makeinfo."
	rm -f *.info* $(INFOS) FAQ.html FAQ.pdf dinotrace.html dinotrace.pdf configure bin/*

clean mostlyclean distclean maintainer-clean maintainer-copy::
	for dir in $(SUBDIRS); do \
	  echo making $@ in $$dir ; \
	  (cd $$dir && $(MAKE) $@) ; \
	done

clean mostlyclean distclean maintainer-clean::
	rm -rf obj_*
	rm -f $(SCRIPTS) *.tmp
	rm -f *.aux *.cp *.cps *.dvi *.fn *.fns *.ky *.kys *.log
	rm -f *.pg *.pgs *.toc *.tp *.tps *.vr *.vrs
	rm -f *.ev *.evs *.ov *.ovs *.cv *.cvs *.ma *.mas

distclean maintainer-clean::
	rm -f Makefile config.status config.cache config.log dinotrace dinotrace.exe TAGS
	rm -rf autom4te.cache

TAGFILES=${srcdir}/src/*.c ${srcdir}/src/*.h ${srcdir}/src/[a-z]*.in \
	${srcdir}/[a-z]*.in ${srcdir}/*.texi

TAGS:	$(TAGFILES)
	etags $(TAGFILES)

tag:
	distname=`sed -e '/DTVERSION/!d' \
	-e 's/[^0-9]*\([0-9.a-z]*\).*/dinotrace_\1/' \
	-e 's/[^A-Za-z0-9]/_/g' \
	-e q src/config.h.in`; \
	svnorcvs tag $$distname

valgrind: dinotrace
	valgrind --suppressions=$(HOME)/.valgrind.supp ./dinotrace

# Don't depend on DISTFILES because there's no rule for "standards.info*".
dist: $(DISTDEP) maintainer-copy
	distname=`sed -e '/DTVERSION/!d' \
	-e 's/[^0-9]*\([0-9.a-z]*\).*/dinotrace-\1/' -e q src/config.h.in`; \
	rm -fr $$distname; \
	mkdir $$distname $$distname/src $$distname/lisp $$distname/traces ; \
        mkdir $$distname/bin $$distname/docs ; \
	for file in $(DISTFILES); do \
	  ln $$file $$distname/$$file \
	  || { echo copying $$file instead; cp -p $$file $$distname/$$file;}; \
	done; \
	chmod -R a+r $$distname; \
	tar chf $$distname.tar $$distname; \
	gzip --force --best $$distname.tar; \
	rm -fr $$distname; \

maintainer-dist: dist tag
	svnorcvs release *.gz

maintainer-diff:
	distname=`sed -e '/DTVERSION/!d' \
	-e 's/[^0-9]*\([0-9.a-z]*\).*/dinotrace_\1/' \
	-e 's/[^A-Za-z0-9]/_/g' \
	-e q src/config.h.in`; \
	svnorcvs diff $$distname

